package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /*
    * 统计今日数据
    * */
    @Override
    public BusinessDataVO getBusinessDate(LocalDateTime begin, LocalDateTime end) {

        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //查询今日总订单
       Integer ordercount =  reportMapper.countBymap(map);

        //查询今日用户增加数量
      Integer newUsers  =  userMapper.userBymap(map);

        map.put("status", Orders.COMPLETED);
        //查询有效订单营业额
      Double  turnover  =  reportMapper.sumBymap(map);
        turnover = turnover==null?0.0:turnover;
      //查询有效已完成订单数量
      Integer validOrderCount =  reportMapper.countBymap(map);
      //计算订单完成率
        Double orderCompletionRate =0.0;
        Double unitPrice =0.0;
        if (validOrderCount>0 && unitPrice>0){
           orderCompletionRate = validOrderCount.doubleValue() / ordercount;
            //计算平均客单价
           unitPrice = turnover / validOrderCount;
        }


        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /*
    * 订单管理
    * */
    @Override
    public OrderOverViewVO getOrdersStutas(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //查询全部订单
       Integer allOrder = reportMapper.countBymap(map);
       //查询带接单订单数量
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = reportMapper.countBymap(map);
       //查询带派送数量
        map.put("status",Orders.CONFIRMED);
       Integer deliverdOrders = reportMapper.countBymap(map);
       //查询已完成数量
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = reportMapper.countBymap(map);
        //查询已取消数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = reportMapper.countBymap(map);

        return  OrderOverViewVO.builder()
                .allOrders(allOrder)
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliverdOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .build();
    }
/*
* 菜品总览
* */
    @Override
    public DishOverViewVO getDishes() {
      Map map = new HashMap();
        //获取停售菜品
        map.put("status",0);
        Integer status1  = dishMapper.getStatus(map);
        //获取起售菜品
        map.put("status",1);
        Integer status0 = dishMapper.getStatus(map);

        return  DishOverViewVO.builder()
                    .sold(status0)
                    .discontinued(status1)
                    .build();
    }
    /*
    * 套餐总览
    * */
    @Override
    public SetmealOverViewVO getSetmeals() {
        Map map = new HashMap();

        //停售
        map.put("status",0);
       Integer stutas0 = setmealMapper.getStutas(map);
       //起售
        map.put("status",1);
        Integer stutas1 = setmealMapper.getStutas(map);

        return SetmealOverViewVO.builder()
                .sold(stutas1)
                .discontinued(stutas0)
                .build();
    }

}
